﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Zhuang.Utility.Common.Conversion;
using Zhuang.Model.Access;
using Zhuang.Utility.WinForm.Event;

namespace Zhuang.WinForm.BasicDataSetting
{
    public partial class ProductAdd : Form
    {
        DAL.Access.ProductUnitDAL productUnitDAL = new DAL.Access.ProductUnitDAL();
        DAL.Access.ProductDAL productDAL = new DAL.Access.ProductDAL();

        int id = -1;

        public ProductAdd()
        {
            InitializeComponent();
        }

        public ProductAdd(int id)
        {
            this.id = id;
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {

            string msg = "商品新增成功！";

            ProductEntity product = new ProductEntity();
            product.ProductCode = tbProductCode.Text.Trim() ;
            product.BarCode = tbBarCode.Text.Trim();
            product.ProductName =tbRetailPrice.Text.Trim() ;
            product.Unit = cbProductUnit.Text.Trim();

            product.RetailPrice = Convert.ToDecimal(tbRetailPrice.Text);
            product.PurchasePrice = Convert.ToDecimal(tbPurchasePrice.Text);
            product.MemberPrice = Convert.ToDecimal(tbMemberPrice.Text);
            product.WholesalePrice = Convert.ToDecimal(tbWholesalePrice.Text);

            product.Description = rtbDescription.Text;
            
            product.SearchCode = PinYinHelper.GetStringSpell(product.ProductName);

            product.CreationDate = DateTime.Now;
            product.ModifiedDate = DateTime.Now;
            product.Status = "Active";

            if (id == -1)
            {
                productDAL.Add(product);

            }
            else
            {
                product.ProductId = id;
                productDAL.Update(product);
                msg = "商品修改成功！";
            }
                          

            DialogResult dr= MessageBox.Show(msg);
            this.DialogResult = DialogResult.Yes;
            this.Close();
           

        }

        private void LoadDefaultData()
        {
            tbProductCode.Text = productDAL.GetProductCode();
            tbRetailPrice.Text = "";
            rtbDescription.Text = "";
            tbRetailPrice.Text = "0.00";
            tbMemberPrice.Text = "0.00";
            tbPurchasePrice.Text = "0.00";
            tbWholesalePrice.Text = "0.00";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ProductAdd_Load(object sender, EventArgs e)
        {

            cbProductUnit.DataSource = productUnitDAL.Get();
            cbProductUnit.DisplayMember = "UnitName";
            cbProductUnit.ValueMember = "UnitName";

            if (id!=-1)
            {
                ProductEntity pe = productDAL.Get(id);

                this.Text = "修改商品";
                
                tbProductCode.Text = pe.ProductCode;
                tbBarCode.Text = pe.BarCode;
                tbProductName.Text = pe.ProductName;

                tbRetailPrice.Text = pe.RetailPrice.ToString() ;
                tbMemberPrice.Text = pe.MemberPrice.ToString();
                tbPurchasePrice.Text = pe.PurchasePrice.ToString();
                tbWholesalePrice.Text = pe.WholesalePrice.ToString();
    
                cbProductUnit.Text = pe.Unit;
                rtbDescription.Text = pe.Description;

            }
            else
            {
                LoadDefaultData();
            }

        }

        private void tbRetailPrice_KeyPress(object sender, KeyPressEventArgs e)
        {

            ControlsEventHelper.KeyPress_NumberInputOnly(sender,e);
        }

        private void tbMemberPrice_KeyPress(object sender, KeyPressEventArgs e)
        {
            ControlsEventHelper.KeyPress_NumberInputOnly(sender, e);
        }

        private void tbPurchasePrice_KeyPress(object sender, KeyPressEventArgs e)
        {
            ControlsEventHelper.KeyPress_NumberInputOnly(sender, e);
        }
    }
}
